<template>
  <div class="app-container">

    <div>
      <el-input v-model="pageDto.where.name" placeholder="请输入设备名称" style="width: 250px;margin-left: 20px;"></el-input>
      <el-select v-model="pageDto.where.industryId" placeholder="请选择设备行业" style="width: 250px;margin-left: 20px;">
        <el-option v-for="item in industryList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
      <el-select v-model="pageDto.where.deviceType" placeholder="请选择设备类型" style="width: 250px;margin-left: 20px;">
        <el-option key="1" label="网关" value="1"></el-option>
        <el-option key="2" label="传感器" value="2"></el-option>
        <el-option key="3" label="继电器" value="3"></el-option>
        <el-option key="4" label="摄像头" value="4"></el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 20px;" @click="pageDto.now = 1;search()">搜索</el-button>
      <el-button type="danger" icon="el-icon-refresh" @click="reset">清空</el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" style="margin-right: 20px;float: right;"
        @click="dialogFormVisible1 = true; deviceType = {}; add()">添加</el-button>
    </div>

    <!-- 页面表格渲染 -->
    <div style="margin-top: 20px;">
      <el-table :data="typeList" style="width: 100%;margin-bottom: 20px;margin-bottom: 20px;"
        :header-cell-style="{ background: '#F7F7F8', color: '#18191B', 'text-align': 'center' }">
        <el-table-column prop="name" label="设备名称类型" align="center"> </el-table-column>
        <el-table-column prop="industryName" label="所属行业" align="center"> </el-table-column>
        <el-table-column label="设备类型" align="center">
          <!-- 自定义渲染 -->
          <template slot-scope="scope">
            <el-tag v-if="scope.row.deviceType == 1" key="网关" type="danger">
              {{ '网关' }}
            </el-tag>
            <el-tag v-if="scope.row.deviceType == 2" key="传感器" type="success">
              {{ '传感器' }}
            </el-tag>
            <el-tag v-if="scope.row.deviceType == 3" key="继电器">
              {{ '继电器' }}
            </el-tag>
            <el-tag v-if="scope.row.deviceType == 4" key="摄像头" type="warning">
              {{ '摄像头' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建日期" :formatter="dateFormat" align="center"></el-table-column>
        <el-table-column prop="updateTime" label="修改日期" :formatter="dateFormat" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-link @click="edit(scope.row)" type="primary" :underline="false">编辑</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageDto.now"
        :page-sizes="[5, 10, 50, 100]" :page-size="pageDto.size" layout="total, sizes, prev, pager, next, jumper"
        :total="total" style="text-align: center;">
      </el-pagination>
    </div>

    <el-dialog title="添加设备类型" :visible.sync="dialogFormVisible1" width="500px">
      <el-form :model="deviceType" :rules="rules" ref="deviceType" label-width="150px" class="demo-ruleForm" size="mini">
        <el-form-item label="设备类型名称" prop="name">
          <el-input v-model="deviceType.name" placeholder="请输入设备类型名称"></el-input>
        </el-form-item>
        <el-form-item label="设备所属行业" prop="industryId">
          <el-select v-model="deviceType.industryId" placeholder="请选择设备行业" style="width: 100%;">
            <el-option v-for="item in industryList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="deviceType.deviceType" placeholder="请选择设备类型" style="width: 100%;">
            <el-option key="1" label="网关" value="1"></el-option>
            <el-option key="2" label="传感器" value="2"></el-option>
            <el-option key="3" label="继电器" value="3"></el-option>
            <el-option key="4" label="摄像头" value="4"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('deviceType')">重置</el-button>
        <el-button @click="dialogFormVisible = false; resetForm('deviceType')">取 消</el-button>
        <el-button type="primary" @click="submitForm('deviceType')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑设备类型" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="deviceType" :rules="rules" ref="deviceType" label-width="150px" class="demo-ruleForm" size="mini">
        <el-form-item label="设备类型名称" prop="name">
          <el-input v-model="deviceType.name" placeholder="请输入设备类型名称"></el-input>
        </el-form-item>
        <el-form-item label="设备所属行业" prop="industryId">
          <el-select v-model="deviceType.industryId" placeholder="请选择设备行业" style="width: 100%;" @change="getIndustryId">
            <el-option v-for="item in industryList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="deviceType.deviceType" placeholder="请选择设备类型" style="width: 100%;" @change="getDeviceType">
            <el-option key="1" label="网关" value="1"></el-option>
            <el-option key="2" label="传感器" value="2"></el-option>
            <el-option key="3" label="继电器" value="3"></el-option>
            <el-option key="4" label="摄像头" value="4"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false; resetForm('deviceType')">取 消</el-button>
        <el-button type="primary" @click="submitForm('deviceType')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { page, industry, type, add } from "@/api/type"
export default {
  data() {
    return {
      rules: {
        name: [
          { required: true, message: '请输入设备类型名称', trigger: 'blur' },
        ],
        industryId: [
          { required: true, message: '请选择设备所属行业', trigger: 'blur' },
        ],
        deviceType: [
          { required: true, message: '请选择设备类型', trigger: 'blur' },
        ],
      },
      dialogFormVisible1: false,
      dialogFormVisible: false,
      typeList: [],
      industryList: [],
      deviceTypeList: [],
      deviceType: {},
      pageDto: {
        now: 1,
        size: 5,
        where: {}
      },
      pages: 0,
      total: 0,
    }
  },
  methods: {
    getIndustryId() {
      this.$nextTick(() => {
        this.$forceUpdate()
      })
    },
    getDeviceType() {
      this.$nextTick(() => {
        this.$forceUpdate()
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          add(this.deviceType).then((res) => {
            if (res.code == 0) {
              this.dialogFormVisible1 = false;
              this.dialogFormVisible = false;
              this.$message({
                message: res.msg,
                type: 'success'
              });
              this.deviceType = {}
              this.search()
            } else {
              this.$message.error(res.msg);
            }
          })
        } else {
          this.$message.error('关键信息未填写！');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    search() {
      page(this.pageDto).then((res) => {
        this.typeList = res.list;
        this.pages = res.pages;
        this.total = res.total;
      });
      industry().then((res) => {
        this.industryList = res
      });
      type().then((res) => {
        this.deviceTypeList = res
      })
    },
    handleSizeChange(size) {
      this.pageDto.size = size;
      this.search();
    },
    handleCurrentChange(now) {
      this.pageDto.now = now;
      this.search();
    },
    //格式日期
    dateFormat(row, column, cellValue, index) {
      const daterc = row[column.property]
      if (daterc) {
        if (daterc.indexOf('T') === -1) return daterc
        const arr = daterc.split('T')
        const d = arr[0]
        const darr = d.split('-')
        const t = arr[1]
        const tarr = t.split('.000')
        const marr = tarr[0].split(':')
        const dd = darr[0] + '-' + darr[1] + '-' + darr[2] + ' ' + marr[0] + ':' + marr[1] + ':' + marr[2].substring(0, 2)
        return dd
      }
    },
    reset() {
      this.pageDto.where = {}
    },
    add() {
      add(deviceType).then((res) => {
        if (res.code == 0) {
          this.dialogFormVisible1 = false;
          this.$message({
            message: '添加成功！',
            type: 'success'
          });
        } else {
          this.$message.error('添加失败！');
        }
      })
    },
    edit(data) {
      this.deviceType.id = data.id
      this.deviceType.industryId = data.industryId
      this.deviceType.name = data.name
      this.deviceType.deviceType = data.deviceType + ''
      this.deviceType.deleted = data.deleted
      this.deviceType.createTime = data.createTime
      this.dialogFormVisible = true
    }
  },
  created() {
    this.search()
  }
}
</script>

<style></style>
